//
//  MSNavigationViewController.m
//  MySchool
//
//  Created by RuiTong_MAC on 16/11/15.
//  Copyright © 2016年 RuiTong_MAC. All rights reserved.
//

#import "MSNavigationViewController.h"

@interface MSNavigationViewController ()<UIGestureRecognizerDelegate, UINavigationControllerDelegate>


@property (strong, nonatomic) UIButton *centerBtn; 

@end

@implementation MSNavigationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
//    [self.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
//    self.navigationBar.backgroundColor = [UIColor clearColor];
//    self.navigationBar.barTintColor = [UIColor clearColor];
//    [self.navigationBar setBackgroundImage:[UIImage imageNamed:@"icon_title"] forBarMetrics:UIBarMetricsDefault];
    
}

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
    
    [super pushViewController:viewController animated:animated]; 
    
    NSArray *childControllerArray = self.childViewControllers;
    if (childControllerArray.count > 0) {
        viewController.hidesBottomBarWhenPushed = YES;
        
        if (viewController.navigationItem.leftBarButtonItem == nil) {
            viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:[self setUIBarButtonItem]];
            viewController.navigationController.interactivePopGestureRecognizer.delegate = (id)self;
            viewController.navigationController.interactivePopGestureRecognizer.enabled = YES;
        } else {
            viewController.navigationController.interactivePopGestureRecognizer.delegate = nil;
            viewController.navigationController.interactivePopGestureRecognizer.enabled = NO;
        }

    }
     
}

- (NSArray<UIViewController *> *)popToViewController:(UIViewController *)viewController
                                            animated:(BOOL)animated {
    
    [super popToViewController:viewController animated:animated];
    
    viewController.navigationController.interactivePopGestureRecognizer.delegate = (id)self;
    viewController.navigationController.interactivePopGestureRecognizer.enabled = YES;
    
    
    
    return @[viewController];
}

#pragma mark - UINavigationControllerDelegate

- (void)navigationController:(UINavigationController *)navigationController
       didShowViewController:(UIViewController *)viewController
                    animated:(BOOL)animate {

    if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.interactivePopGestureRecognizer.enabled = YES;
    }
    
}


- (UIButton *)setUIBarButtonItem {
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [backButton setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    [backButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [backButton setTitleColor:[UIColor orangeColor] forState:UIControlStateHighlighted];
    [backButton addTarget:self action:@selector(goBack) forControlEvents:UIControlEventTouchUpInside];

    [backButton sizeToFit];

    backButton.contentEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);

    return backButton;
}

- (void)goBack {
     
    [self popViewControllerAnimated:YES];
}


@end
